<?php
$GLOBALS['PAGE_FORM_DATA'] = array();
$act = isset($_REQUEST['act'])?$_REQUEST['act']:'';
$dt = new DataTable(constant('DB_TABLE_ARTICLE'));
$GLOBALS['GV_IMAGE_PATH'] = 'article/';
$dt_image =  new DataTable(constant('DB_TABLE_ARTICLE_IMAGE'));
if ( $act == 'save') {
	$article_data = $_REQUEST;
	//
	if ( intval($article_data['id']) > 0 ) {
		/*
		//contacta
		if ( $article_data['display_style_id'] == 1 ) {
			contact_add_php_removeArticleImage(sprintf("`article_id`=%d AND `type`=%d",$article_data['id'], 2));
		}
		//contactb
		else if ( $article_data['display_style_id'] == 2 ) {
			contact_add_php_removeArticleImage(sprintf("`article_id`=%d AND `type`=%d",$article_data['id'], 1));
			contact_add_php_removeArticleImage(sprintf("`article_id`=%d AND `type`=%d",$article_data['id'], 2));
		}
		//contactc
		else if ( $article_data['display_style_id'] == 3 ){
			contact_add_php_removeArticleImage(sprintf("`article_id`=%d AND `type`=%d",$article_data['id'], 1));
		}
		*/
	}
	
	if ( strlen($article_data['ajaxf_image']) > 0) {
		$GLOBALS['APP_INSTANCE']->saveUploadedFile($article_data['ajaxf_image'], $GLOBALS['GV_IMAGE_PATH'] );
		$article_data['img_path'] = $article_data['ajaxf_image'];
	}
	if ( isset($article_data['meta_keyword']) AND is_array($article_data['meta_keyword']) ) {
		$article_data['meta_keyword'] = implode(',', $article_data['meta_keyword'] );
	}else{
		$article_data['meta_keyword'] = '';
	}
	$dt->save($article_data);
	if ( strlen($article_data['a_style_img']) > 0) {
		$style_a_img_list = explode(',', $article_data['a_style_img']);
		foreach($style_a_img_list as $tmp_img_name)
		{
			$GLOBALS['APP_INSTANCE']->saveUploadedFile($tmp_img_name, $GLOBALS['GV_IMAGE_PATH'] );
			$article_image_data = array(
					'article_id'=>$article_data['id'],
					'type'=>'1',
					'path'=>$tmp_img_name
					);
			$dt_image->save($article_image_data);
		}
	}
	
	if ( strlen($article_data['ajaxf_image2']) > 0) {
		$GLOBALS['APP_INSTANCE']->saveUploadedFile($article_data['ajaxf_image2'], $GLOBALS['GV_IMAGE_PATH'] );
		$article_image_data = array(
				'article_id'=>$article_data['id'],
				'type'=>'2',
				'path'=> $article_data['ajaxf_image2']
		);
		$article_image_data = $dt_image->get_row(sprintf("`article_id`=%d and `type`=2",$article_data['id']));
		if ( !$article_image_data ) {
			$article_image_data = array(
					'article_id'=>$article_data['id'],
					'type'=>'2'
			);
		}
		$article_image_data['path'] = $article_data['ajaxf_image2'];
		$dt_image->save($article_image_data);
	}
	ob_clean();
	$redirect_url = 'news.php';
	if ( $article_data['cat_id'] == 1) {
		$redirect_url = 'people.php';
	}
	$redirect_url .= '?sub_cat_id='.$article_data['sub_cat_id'];
	header('Location:'.$redirect_url);
	exit();
}
else if ( $act == 'edit') {
	$GLOBALS['PAGE_FORM_DATA'] = $dt->get(intval($_REQUEST['id']));
	$GLOBALS['PAGE_FORM_DATA']['a_style_img'] = array();
	if ( $GLOBALS['PAGE_FORM_DATA']['display_style_id'] == 1 ){
		$GLOBALS['PAGE_FORM_DATA']['a_style_img'] = $dt_image->get_list(sprintf("`article_id`=%d and `type`=1",$_REQUEST['id']), 'id');
	}
	if ($GLOBALS['PAGE_FORM_DATA']['cat_id'] == 2 && $GLOBALS['PAGE_FORM_DATA']['sub_cat_id'] == 5 ){
		$db_row = $dt_image->get_row(sprintf("`article_id`=%d and `type`=2",$GLOBALS['PAGE_FORM_DATA']['id']));
		$GLOBALS['PAGE_FORM_DATA']['img2_path'] = $db_row['path'];
	}
	
	$GLOBALS['PAGE_FORM_DATA']['meta_keyword'] = explode(',', $GLOBALS['PAGE_FORM_DATA']['meta_keyword'] );
}
else if ( $act == 'delete_a_style_img' ) {
	$dt_image =  new DataTable(constant('DB_TABLE_ARTICLE_IMAGE'));
	$row = $dt_image->get($_REQUEST['id']);
	if ( $row ) {
		$GLOBALS['APP_INSTANCE']->removeFile($row['path'], $GLOBALS['GV_IMAGE_PATH']);
		$dt_image->remove($row['id']);
	}
	ob_clean();
	$redirect_url = 'contact-add.php?';
	$redirect_url .= '&sub_cat_id='.$_REQUEST['sub_cat_id'];
	$redirect_url .= '&act=edit&id='.$row['article_id'];
	header('Location:'.$redirect_url);
	exit();
}
else{
	$GLOBALS['PAGE_FORM_DATA'] = $_REQUEST;
	$GLOBALS['PAGE_FORM_DATA']['id'] = 0;
	if ( !isset($GLOBALS['PAGE_FORM_DATA']['cat_id']) )
		$GLOBALS['PAGE_FORM_DATA']['cat_id'] = 0;
	if ( !isset($GLOBALS['PAGE_FORM_DATA']['display_style_id']) )
		$GLOBALS['PAGE_FORM_DATA']['display_style_id'] = 1;
	if ( !isset($GLOBALS['PAGE_FORM_DATA']['sub_cat_id']) )
		$GLOBALS['PAGE_FORM_DATA']['sub_cat_id'] = 0;
	
	$GLOBALS['PAGE_FORM_DATA']['title'] = '';
	$GLOBALS['PAGE_FORM_DATA']['content'] = '';
	$GLOBALS['PAGE_FORM_DATA']['date'] = $GLOBALS['APP_INSTANCE']->formatDate();
	$GLOBALS['PAGE_FORM_DATA']['image_path'] = false;
	$GLOBALS['PAGE_FORM_DATA']['image2_path'] = false;
	$GLOBALS['PAGE_FORM_DATA']['meta_keyword'] = array();
	$GLOBALS['PAGE_FORM_DATA']['meta_description'] = '';
	$GLOBALS['PAGE_FORM_DATA']['a_style_img'] = array();
	
}

function contact_add_php_removeArticleImage($condition)
{
	$dt_image =  new DataTable(constant('DB_TABLE_ARTICLE_IMAGE'));
	$list_data = $dt_image->get_list($condition);
	foreach($list_data as $row)
	{
		$GLOBALS['APP_INSTANCE']->removeFile($row['path'], $GLOBALS['GV_IMAGE_PATH']);
		$dt_image->remove($row['id']);
	}
}
?>